package de.stefanocke.roo.wicket.templates

import de.stefanocke.roo.wicket.Stereotypes

class DataProviderTemplate extends JavaHtmlTemplate{
	
	override String javaClassNameWhenNew(){
		"DataProviderBase"
	}
	
	override String stereotype(){
		Stereotypes::DATA_PROVIDER
	}
	
	override subPackageToUseWhenNew(){
		"common"
	}
	
	override buildJavaContents(){
		import_("org.apache.wicket.extensions.markup.html.repeater.data.sort.ISortState")
		import_("org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider")
		import_("org.apache.wicket.extensions.markup.html.repeater.util.SingleSortState")

		'''
		/**
		 * A data provider that might be sortable or not. Base class for all DataProviders. 
		 */
		«stereotypeAnnotation» 
		public abstract class «simpleTypeName»<T> implements ISortableDataProvider<T> {
			
			private static final long serialVersionUID = 1L;
			
			private ISortState sortState = new SingleSortState();
			
			public DataProviderBase() {
				super();
			}
			
			/**
			 * {@inheritDoc}
			 * If the DataProvider does not support sorting, null is returned here.
			 */
			@Override
			public ISortState getSortState() {
				if(isSortable()){
					return sortState;
				} else {
					return null;
				}
			}
			
			/**
			 * To be overridden by subclasses.
			 * @return true if the DataProvider supports sorting.
			 */
			public boolean isSortable(){
				return false;
			}
		}
		'''
	}
	
	
}